/****************************************************
* Unity版本：2022.1.21f1c1
* Version：0.1
* Company：DefaultCompany
* 日期：2023/06/20 17:09:21
*****************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MqttUIManager : MonoBehaviour
{
    private int clinetIDStart = 123456;
    public Transform canvas;
    public ClientUI clientTestPrefab;
    public Button clientBtnPrefab;
    public Transform clientParent;
    public List<ClientUI>  clientArray =new List<ClientUI>();
    public List<Button> clientButtonArray = new List<Button>();
    public Button addClientButton;
    public Sprite normalSprite;
    public Sprite pressSprite;
    void Start()
    {
        addClientButton.onClick.AddListener(AddClient);
    }


    public void AddClient()
    {
        string clientid = (clinetIDStart++).ToString();
        Button clientBtn = Instantiate<Button>(clientBtnPrefab, clientParent);
        clientBtn.transform.SetSiblingIndex(clientParent.childCount-2);
        clientButtonArray.Add(clientBtn);
        ClientUI clientUI = Instantiate<ClientUI>(clientTestPrefab, canvas);

        clientBtn.onClick.AddListener(() => { ClientButtonCLick(clientBtn, clientUI, clientid); });
        clientBtn.onClick.Invoke();
    }
    private Button currentClientBtn;
    private ClientUI currentClientUI;
    private void ClientButtonCLick(Button clientBtn, ClientUI clientUI,string clientId) {
        clientUI.ClientID = clientId;
        clientBtn.GetComponentInChildren<Text>().text = clientId;
        if (currentClientBtn!=null) {
            currentClientBtn.GetComponent<Image>().sprite =normalSprite;
            currentClientUI.gameObject.SetActive(false);
          
        }
        currentClientBtn = clientBtn;
        currentClientUI = clientUI;
        currentClientBtn.gameObject.SetActive(true);
        currentClientBtn.GetComponent<Image>().sprite = pressSprite;
        currentClientUI.gameObject.SetActive(true);
    }
}
